<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
            <el-form-item label="卡券类型编码" prop="typeCode">
                <el-input v-model="queryParams.typeCode" placeholder="请输入卡券类型编码" maxlength="10" show-word-limit
                    clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="卡券类型名称" prop="typeName">
                <el-select v-model="queryParams.typeName">
                    <el-option v-for="item in typeList" :key="item.id" :label="item.name" :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="是否能兑换电影启用" prop="movieEnabled">
                <el-select v-model="queryParams.movieEnabled">
                    <el-option v-for="item in enabledList" :key="item.id" :label="item.name" :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="是否能兑换福利" prop="welfareEnabled">
                <el-select v-model="queryParams.welfareEnabled">
                    <el-option v-for="item in enabledList" :key="item.id" :label="item.name" :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="是否启用" prop="enabled">
                <el-select v-model="queryParams.enabled">
                    <el-option v-for="item in enabledList1" :key="item.id" :label="item.name" :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd"
                    v-hasPermi="['film:cardType:add']">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
                    v-hasPermi="['film:cardType:remove']">删除</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table border v-loading="loading" :data="cardTypeList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="序号" align="center" width="55" type="index" />
            <el-table-column label="卡券类型编码" align="center" prop="typeCode" min-width="150"
                :show-overflow-tooltip="true" />
            <el-table-column label="卡券类型名称" align="center" prop="typeName" min-width="100"
                :show-overflow-tooltip="true">
                <template slot-scope="scope">
                    <div v-if="scope.row.typeName == 1">观影次数卡</div>
                    <div v-if="scope.row.typeName == 2">积分卡</div>
                </template>
            </el-table-column>
            <el-table-column label="计量方式" align="center" prop="cardMode" min-width="100" :show-overflow-tooltip="true">
                <template slot-scope="scope">
                    <div v-if="scope.row.cardMode == 1">观影次数卡</div>
                    <div v-if="scope.row.cardMode == 2">积分卡</div>
                </template>
            </el-table-column>
            <!-- <el-table-column label="卡图路径" align="center" prop="cardImagePath" /> -->
            <el-table-column label="最大抵扣额度" align="center" prop="quotaLimit" min-width="100"
                :show-overflow-tooltip="true" />
            <el-table-column label="积分额度" align="center" prop="integralLimit" min-width="100"
                :show-overflow-tooltip="true" />
            <el-table-column label="次数额度" align="center" prop="maxRedeemTimes" min-width="100"
                :show-overflow-tooltip="true" />
            <el-table-column label="生成后默认有效期(天)" align="center" prop="validDays" min-width="150"
                :show-overflow-tooltip="true" />
            <el-table-column label="是否能兑换电影启用" align="center" prop="movieEnabled" min-width="150"
                :show-overflow-tooltip="true">
                <template slot-scope="scope">
                    <el-switch v-model="scope.row.movieEnabled" :active-value="0" :inactive-value="1"
                        @change="handleStatusChange(scope.row, 'movieEnabled')"></el-switch>
                </template>
            </el-table-column>
            <el-table-column label="是否能兑换福利" align="center" prop="welfareEnabled" min-width="150"
                :show-overflow-tooltip="true">
                <template slot-scope="scope">
                    <el-switch v-model="scope.row.welfareEnabled" :active-value="0" :inactive-value="1"
                        @change="handleStatusChange(scope.row, 'welfareEnabled')"></el-switch>
                </template>
            </el-table-column>
            <el-table-column label="是否启用" align="center" prop="enabled" min-width="100" :show-overflow-tooltip="true">
                <template slot-scope="scope">
                    <el-switch v-model="scope.row.enabled" :active-value="0" :inactive-value="1"
                        @change="handleStatusChange(scope.row, 'enabled')"></el-switch>
                </template>
            </el-table-column>
            <el-table-column label="类型说明" align="center" prop="description" min-width="200"
                :show-overflow-tooltip="true" />
            <el-table-column label="排序" align="center" prop="sort" min-width="80" :show-overflow-tooltip="true" />
            <el-table-column label="备注" align="center" prop="remark" min-width="150" :show-overflow-tooltip="true" />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="120">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" @click="handleUpdate(scope.row)"
                        v-hasPermi="['film:cardType:edit']">修改</el-button>
                    <el-button size="mini" type="text" @click="handleDelete(scope.row)"
                        v-hasPermi="['film:cardType:remove']">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize" @pagination="getList" />

        <!-- 添加或修改卡券类型对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
            <el-form class="addData" :inline="true" ref="form" :model="form" :rules="rules" label-width="130px">
                <el-form-item label="卡券类型编码" prop="typeCode">
                    <el-input v-model="form.typeCode" placeholder="请输入卡券类型编码" maxlength="10" show-word-limit />
                </el-form-item>
                <el-form-item label="卡券类型名称" prop="typeName">
                    <el-select v-model="form.typeName">
                        <el-option v-for="item in typeList" :key="item.id" :label="item.name" :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="计量方式" prop="cardMode">
                            <el-select v-model="form.cardMode">
                                <el-option v-for="item in typeList" :key="item.id" :label="item.name" :value="item.id">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="最大抵扣额度" prop="quotaLimit" v-if="form.cardMode == 1">
                            <el-input-number v-model="form.quotaLimit" :precision="2" :step="0.1" :max="1000" :min="1"
                                :controls="false" placeholder="请输入每次最大抵扣额度，仅次数卡使用，单位元"></el-input-number>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="积分额度" prop="integralLimit" v-if="form.cardMode == 2">
                            <el-input-number :controls="false" v-model="form.integralLimit" :step="1" step-strictly
                                placeholder="请输入积分额度" :min="1"></el-input-number>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="次数额度" prop="maxRedeemTimes" v-if="form.cardMode == 1">
                            <el-input-number :controls="false" v-model="form.maxRedeemTimes" :step="1" step-strictly
                                placeholder="请输入次数额度" :min="1"></el-input-number>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="激活后有效期(天)" prop="validDays">
                            <el-input-number :controls="false" v-model="form.validDays" :step="1" step-strictly
                                placeholder="请输入激活后有效期" :min="1"></el-input-number>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="是否能兑换电影" prop="movieEnabled">
                            <el-select v-model="form.movieEnabled">
                                <el-option v-for="item in enabledList" :key="item.id" :label="item.name"
                                    :value="item.id">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="是否能兑换福利" prop="welfareEnabled">
                            <el-select v-model="form.welfareEnabled">
                                <el-option v-for="item in enabledList" :key="item.id" :label="item.name"
                                    :value="item.id">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="是否启用" prop="enabled">
                            <el-select v-model="form.enabled">
                                <el-option v-for="item in enabledList1" :key="item.id" :label="item.name"
                                    :value="item.id">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="排序值" prop="sort">
                            <el-input-number :controls="false" v-model="form.sort" :step="1" step-strictly
                                placeholder="请输入排序值" :min="1"></el-input-number>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="24">
                        <el-form-item label="卡图路径" prop="cardImagePath">
                            <image-upload v-model="form.cardImagePath" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="24">
                        <el-form-item label="类型说明" prop="description">
                            <el-input class="longData" v-model="form.description" type="textarea" placeholder="请输入内容"
                                rows="3" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="24">
                        <el-form-item label="备注" prop="remark">
                            <el-input class="longData" v-model="form.remark" type="textarea" placeholder="请输入内容"
                                rows="3" />
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { listCardType, getCardType, delCardType, addCardType, updateCardType } from "@/api/cardType/cardType";

export default {
    name: "CardType",
    data() {
        return {
            enabledList1: [{
                id: 0,
                name: '启用'
            },
            {
                id: 1,
                name: '停用'
            }],

            enabledList: [
                {
                    id: 0,
                    name: '能'
                },
                {
                    id: 1,
                    name: '不能'
                }
            ],
            typeList: [
                {
                    id: '1',
                    name: '观影次数卡'
                },
                {
                    id: '2',
                    name: '积分卡'
                }
            ],
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 卡券类型表格数据
            cardTypeList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                typeCode: null,
                typeName: null,
                cardMode: null,
                cardImagePath: null,
                quotaLimit: null,
                integralLimit: null,
                maxRedeemTimes: null,
                validDays: null,
                movieEnabled: null,
                welfareEnabled: null,
                enabled: null,
                description: null,
                sort: null,
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                typeCode: [
                    { required: true, message: "卡券类型编码不能为空", trigger: "blur" }
                ],
                typeName: [
                    { required: true, message: "卡券类型名称不能为空", trigger: "blur" }
                ],
                cardMode: [
                    { required: true, message: "计量方式不能为空", trigger: "blur" }
                ],
                cardImagePath: [
                    { required: true, message: "卡图不能为空", trigger: "blur" }
                ],
                quotaLimit: [
                    { required: true, message: "最大抵扣额度不能为空", trigger: "blur" }
                ],
                integralLimit: [
                    { required: true, message: "积分额度不能为空", trigger: "blur" }
                ],
                maxRedeemTimes: [
                    { required: true, message: "次数额度不能为空", trigger: "blur" }
                ],
                validDays: [
                    { required: true, message: "激活后默认有效期不能为空", trigger: "blur" }
                ],

                movieEnabled: [
                    { required: true, message: "是否能兑换电影不能为空", trigger: "blur" }
                ],
                welfareEnabled: [
                    { required: true, message: "是否能兑换福利不能为空", trigger: "blur" }
                ],
                enabled: [
                    { required: true, message: "是否启用不能为空", trigger: "blur" }
                ],
                sort: [
                    { required: true, message: "排序不能为空", trigger: "blur" }
                ],
            }
        };
    },
    created() {
        this.getList();
    },
    methods: {
        handleStatusChange(row, field) {
            const originalValue = row[field]; // 保存原始值以便回滚
            this.$modal.confirm('确认要修改此状态吗').then(() => {
                return updateCardType({
                    id: row.id,
                    [field]: row[field] // 使用动态属性名
                });
            }).then(() => {
                this.$modal.msgSuccess("修改成功");
                this.getList();
            }).catch(() => {
                // 回滚到原始值
                row[field] = originalValue === 0 ? 1 : 0;
            });
        },
        /** 查询卡券类型列表 */
        getList() {
            this.loading = true;
            listCardType(this.queryParams).then(response => {
                this.cardTypeList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                typeCode: null,
                typeName: null,
                cardMode: null,
                cardImagePath: null,
                quotaLimit: null,
                integralLimit: null,
                maxRedeemTimes: null,
                validDays: null,
                movieEnabled: null,
                welfareEnabled: null,
                enabled: null,
                description: null,
                sort: null,
                delFlag: null,
                createBy: null,
                createTime: null,
                updateBy: null,
                updateTime: null,
                remark: null
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加卡券类型";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.id || this.ids
            getCardType(id).then(response => {
                this.form = response.data;
                this.open = true;
                this.title = "修改卡券类型";
            });
        },
        /** 提交按钮 */
        submitForm() {
            if (this.form.cardMode == 1) {
                delete this.form.integralLimit
            }
            if (this.form.cardMode == 2) {
                delete this.form.quotaLimit
                delete this.form.maxRedeemTimes
            }
            this.$refs["form"].validate(valid => {
                if (valid) {

                    if (this.form.id != null) {
                        updateCardType(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addCardType(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal.confirm('是否确认删除卡券类型编号为"' + ids + '"的数据项？').then(function () {
                return delCardType(ids);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => { });
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('film/cardType/export', {
                ...this.queryParams
            }, `cardType_${new Date().getTime()}.xlsx`)
        }
    }
};
</script>
